package com.hqbzl.c1.volatiles;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class TestSyunc {
    static int a = 0;
    final static Object lock = new Object();

    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (lock) {
                while (a == 0) {
                    try {
                        log.debug("t1 wait");
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("a = " + a);
            }
        }, "t1").start();
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            synchronized (lock) {
                a = 10;
                log.debug("t2 next");
                lock.notify();
            }
        }, "t2").start();
    }
}
